package com.yh.aidl_client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yh.aidl_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button button;
    IMyAidlInterface mStub;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.yh.aidl_server","com.yh.aidl_server.Server_service"));
        bindService(intent,mServiceConnection,BIND_AUTO_CREATE);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mStub = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                int add = mStub.add(4, 5);
               TextView mTv = findViewById(R.id.tv1);
                mTv.setText(add+"");
                Log.d("add",add+"");
            } catch (RemoteException e) {
                Log.d("yonghao",e.toString());
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
}
